package com.ecommerce.user.entity;

import jakarta.persistence.*;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户实体 - 不继承BaseEntity，避免ID生成策略冲突
 */
@Data
@Entity
@Table(name = "t_user")
@EntityListeners(AuditingEntityListener.class)
public class User {

    /**
     * 主键ID - 手动设置，不自动生成
     */
    @Id
    @Column(name = "id")
    private Long id;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 用户名
     */
    @Column(nullable = false, unique = true, length = 50)
    private String username;

    /**
     * 密码
     */
    @Column(nullable = false)
    private String password;

    /**
     * 邮箱
     */
    @Column(nullable = false, unique = true)
    private String email;

    /**
     * 手机号
     */
    @Column(length = 20)
    private String phone;

    /**
     * 地址
     */
    @Column(name = "address", length = 500)
    private String address;

    /**
     * 头像URL
     */
    @Column(length = 500)
    private String avatar;

    /**
     * 昵称
     */
    @Column(length = 50)
    private String nickname;

    /**
     * 用户角色
     */
    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "t_user_role", joinColumns = @JoinColumn(name = "user_id"))
    @Column(name = "role")
    private Set<String> roles = new HashSet<>();

    /**
     * 用户类型（USER, ADMIN, VIP等）
     */
    @Column(name = "user_type", length = 20, nullable = false)
    private String userType = "USER";

    /**
     * 用户状态（启用/禁用）
     */
    @Column(name = "enabled", nullable = false)
    private Boolean enabled = true;
}